﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;

namespace MidTermWorkWPF.MyCommands
{
    /// <summary>
    /// Summary description for MyLayerVisibleCommandClass.
    /// </summary>
    [Guid("aebd14a9-46ee-484f-85ef-3226d091ae49")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("MidTermWorkWPF.MyToolbarMenuItems.MyMapToolbarMenuCommands.MyLayerVisibleCommand.MyLayerVisibleCommandClass")]
    public sealed class MyLayerVisibleCommandClass : BaseCommand
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper;

        public MyLayerVisibleCommandClass()
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = "LayerVisible"; //localizable text
            base.m_caption = "LayerVisible";  //localizable text
            base.m_message = "Make all layer visible";  //localizable text 
            base.m_toolTip = "AllLayerVisible";  //localizable text 
            base.m_name = "MyLayerVisible";   //unique id, non-localizable (e.g. "MyCategory_MyCommand")

            try
            {
                //
                // TODO: change bitmap name if necessary
                //
                //string bitmapResourceName = GetType().Name + ".bmp";
                //base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);

                // 获取程序集中的资源数组
                string[] res = GetType().Assembly.GetManifestResourceNames();
                // 遍历资源数组，找到你想要的图标文件
                foreach (string r in res)
                {
                    if (r.Contains(GetType().Name + ".bmp"))
                    {
                        // 设置工具属性
                        base.m_bitmap = new Bitmap(GetType().Assembly.GetManifestResourceStream(r));
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overridden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();

            m_hookHelper.Hook = hook;

            // TODO:  Add other initialization code
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add MyLayerVisibleCommandClass.OnClick implementation
            ICompositeLayer pCompositeLayer;
            ILayer mCompositeLayer;
            IMap mMap = m_hookHelper.FocusMap;
            IActiveView activeView = m_hookHelper.ActiveView;
            for (int i = 0; i < mMap.LayerCount; i++)  //遍历所有图层
            {
                if (mMap.get_Layer(i) is ICompositeLayer)
                {
                    pCompositeLayer = mMap.get_Layer(i) as ICompositeLayer;
                    for (int j = 0; j < pCompositeLayer.Count; j++)
                    {
                        mCompositeLayer = pCompositeLayer.get_Layer(j);
                        mCompositeLayer.Visible = true;
                    }
                    mMap.get_Layer(i).Visible = true;
                }
                else
                {
                    mCompositeLayer = mMap.get_Layer(i);
                    mCompositeLayer.Visible = true;
                }
            }
            //axTOCControl1.Update();
            activeView.Refresh();
        }

        #endregion
    }
}
